var CurrentBase = Class.create();

CurrentBase.prototype = {
    initialize : function(im) {
        this.im = im;
        this.friends_url = '/link-me-in/' + im + '/friends'
        this.invite_url = '/link-me-in/' + im + '/invite'
        this.login_url = '/link-me-in/' + im + '/index'
    },
    _add : function(element, employee, attribute) {
        if (element.disabled) {
            return;
        }
        element.disabled = true

        var loadingImg;
        var messageSpan;
        element.ancestors()[1].childElements().each(function(item) {
            if (item.hasClassName("notice")) {
                loadingImg = item.childElements().first();
                messageSpan = item.childElements().last();
            }
        });
        var current_base = this;
        new Ajax.Request(this.invite_url, {
            method: 'post',
            parameters : 'id=' + employee[attribute],

            onCreate: function() {
                element.addClassName("disable");
                loadingImg.setStyle({visibility: "visible"});
                messageSpan.setStyle({visibility: "visible"});
                messageSpan.update('Adding ' + employee.getDisplayName() + ' to your ' + current_base.im + ' ...');
            },

            on401 : function() {
                messageSpan.update('Not logged in, redirecting to login page.');
                window.location = current_base.login_url;
            },

            on200: function() {
                loadingImg.setStyle({visibility: "hidden"});
                messageSpan.update('Successfully add ' + employee.getDisplayName() + ' to your ' + current_base.im + '.');
            },

            on500: function() {
                loadingImg.setStyle({visibility: "hidden"});
                messageSpan.update('Fail to add ' + employee.getDisplayName() + ' to your ' + current_base.im + '.');
            },

            onComplete: function() {
                element.removeClassName("disable");
                element.disabled = false;
            }
        });
    },
    _load_friends : function(class_name, imid, originalText) {
        var current = this;

        new Ajax.Request(this.friends_url, {
            method: 'get',
            onCreate: function() {
                var allbuttons = $$(class_name);
                allbuttons.each(function(button) {
                    button.update('loading');
                });
            },
            on401 : function() {
                messageSpan.update('Not logged in, redirecting to login page.');
                window.location = current_base.login_url;
            },

            on200: function(transport) {
                var friends = transport.responseText.evalJSON();
                var allbuttons = $$(class_name);
                if (!friends || friends.length == 0) {
                    allbuttons.each(function(button) {
                        button.update(originalText)
                    });
                }
                allbuttons.each(function(button) {
                    if (!button.title) {
                        button.update(originalText)
                        return;
                    }
                    var idOfFriends = button.title;
                    if (friends.indexOf(idOfFriends) != -1) {
                        button.onclick = function(){alert('this function will come in next release.')};
                        button.update('&gt;Talk')
                    } else {
                        button.update(originalText)
                    }
                });
            }
        });
    }
}

